<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="1100px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="设备名称" prop="machineName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.machineName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="machineId">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.machineId"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.loading" size="default" index :data="dataList">
        <el-table-column label="设备名称" show-overflow-tooltip align="center" prop="machineName" />
        <el-table-column label="设备编号" show-overflow-tooltip align="center" prop="machineSn" />
        <el-table-column label="累计用户数" align="center" prop="totalUser">
          <template #default="{ row }">
            <div>
              <el-link
                :underline="false"
                :disabled="!hasPermi(['org:user:total'])"
                type="primary"
                @click="handleUser(row, '累计用户数')"
                >{{ row.totalUser }}</el-link
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="下单用户数" align="center" prop="orderUser">
          <template #default="{ row }">
            <div>
              <el-link
                :underline="false"
                type="primary"
                :disabled="!hasPermi(['org:user:order'])"
                @click="handleUser(row, '下单用户数')"
                >{{ row.orderUser }}</el-link
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="新用户数" align="center" prop="newUser">
          <template #default="{ row }">
            <div>
              <el-link
                :underline="false"
                type="primary"
                :disabled="!hasPermi(['org:user:new'])"
                @click="handleUser(row, '新用户数')"
                >{{ row.newUser }}</el-link
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="新用户环比" align="center" prop="newUserSequentialGrowth">
          <template #default="{ row }"> {{ row.newUserSequentialGrowth || '0.00' }}% </template>
        </el-table-column>
        <el-table-column label="新用户占比" align="center" prop="newUserRatio">
          <template #default="{ row }"> {{ row.newUserRatio || '0.00' }}% </template>
        </el-table-column>
        <el-table-column label="复购率" align="center" prop="repurchaseRate">
          <template #default="{ row }"> {{ row.repurchaseRate || '0.00' }}% </template>
        </el-table-column>
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="state.total > 0"
          :total="state.total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <userList ref="userListRef"></userList>
    </div>
  </el-dialog>
</template>

<script setup>
import { hasPermi } from '@/utils/tools'
import userList from './userList.vue'
import { getItemMachineListApi } from '@/api/statistic'
const queryRef = ref(null)

const dataList = ref([])
const visible = ref(false)
const title = ref('')

const queryParams = reactive({
  startDate: '',
  endDate: '',
  machineName: '',
  machineId: '',
  positionId: '',
  pageNum: 1,
  pageSize: 10
})
const state = reactive({
  total: 0,
  loading: false
})

/** 查询用户列表 */
async function getList() {
  state.loading = true

  try {
    const r = await getItemMachineListApi(queryParams)
    dataList.value = r?.data?.rows ?? []
    state.total = r?.data?.total ?? 0
  } finally {
    state.loading = false
  }
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

const userListRef = ref(null)
function handleUser(row, name) {
  userListRef.value.init(row, name, queryParams)
}

function init(row, params) {
  console.log('params', params)
  visible.value = true
  queryParams.positionId = row.positionId
  title.value = row.positionName
  queryParams.startDate = params.startDate
  queryParams.endDate = params.endDate
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.type {
  margin-bottom: 20px;
}
</style>
